import { useSyncExternalStore } from "react"

export default function useOnline() {
  const isOnline = useSyncExternalStore(
    callback => {
      window.addEventListener('online', callback)
      window.addEventListener('offline', callback)
      return () => {
        window.removeEventListener('online', callback)
        window.removeEventListener('offline', callback)
      }
    },
    () => navigator.onLine
  )
  return isOnline
}